<?php
/**
 * 权限
 */
namespace app\model;

use think\Model;

use think\Facade\Db;
use app\model\AdminRole;

class Chain extends Model
{
    /**
     * 
     */
    public $ids=[];
    /**
     * 当前产业链详情
     */
    public function GetChainDetail($id){
      $data = Chain::find($id);
      return  $data;
    }
     /**
     * 通过id获得产业链id
     */
    public function GetChainIdById($id){
     
      $res = Chain::where(['id'=>$id])->value('chain_id');
      return  $res ;
    }
    /**
     * 通过用户id获得产业链id
     */
    public function GetChainListByUserId($user_id){
    	if(empty($user_id)){
    		return [];
    	}
    	$AdminRole=new AdminRole();
    	$RoleId=$AdminRole->GetRoleIdByAdminId($user_id);
    	
    	if(empty($RoleId)){
    		return [];
    	}
    	
    	$res = Chain::where(['role_id'=>$RoleId,'pid'=>0])->column('chain_id');
    	return  $res ;
    }
    /**
     * 通过id获取当前以及下级节点信息
     */
    public function GetChainChildrenListById($id=0,$user_id=0,$imp=true){
    	if(empty($user_id)){
    		//return [];
    	}
    	$AdminRole=new AdminRole();
    	$RoleId=$AdminRole->GetRoleIdByAdminId($user_id);
    	
    	if(empty($RoleId)){
    		$res = Chain::select()->toArray();
    	}else{
    		$res = Chain::where(['role_id'=>$RoleId])->select()->toArray();
    	}
    	$res=arr2tree($res,$id,'chain_name','id');
    	if($imp){
    		$res=imp($res);
    	}
    	return  $res ;
    }
    
   /**
    * 角色对应产业链列表
    *
    */
    public function GetChain($roleid,$where='1=1',$field="id as insertid,chain_id as id,chain_name,pid"){
    	$list = Chain::field($field)->where('role_id', $roleid)->where($where)->select()->toArray();
    	return  $list;
    }
  /**
   * 处理产业链关系
   */
  public function DealRel($id,$data=array()){ 
    
     Chain::where('role_id','=',$id)->delete();
     
     $this->AddUpdate($id,$data,0);

     return true;

  }
  /**
   * 递归插入\修改
   */
  protected function AddUpdate($roleid,$data=array(),$pid=0){
    
     foreach($data as $val){
       
        $chain           = new Chain;
        $chain->role_id     = $roleid;
        $chain->chain_id    = $val['id'];
        $chain->chain_name    = $val['chain_name'];       
        $chain->pid    = $pid;       
        $chain->save();
        // 获取自增ID
        if(isset($val['children'])&&!empty($val['children'])){
            
            $this->AddUpdate($roleid,$val['children'],$chain->id);
        }
     }
     return true;
  }
  /**
   * 将子节点全部存在父级节点
   */
  public function UpdatePoint($roleid){
     $list = Chain::where('role_id', $roleid)->field('id as insertid,chain_id as id,chain_name,pid')->select()->toArray();
     $data=arr2tree($list,0,'chain_name');
    
     $res=$this->ContaintChildren($data);
     return $res;
     //return $data;
  }

  /**
   * 获取并修改带有所有子类含自己
   */
  protected function ContaintChildren($data){
     foreach ($data as $key => $val) {
         # code...
         $obj=[];
         //$val['value'][]=$val['id'];
         $this->ids=[$val['id']];
         if(isset($val['children'])&&!empty($val['children'])){         
            $this->ChildrenGui($val['children']);              
         }
         //p($this->ids);
         $chain = Chain::find($val['insertid']);
         $chain->children     = implode(',',$this->ids);
         $chain->save();
         $data[$key]['ChildrenChain']=implode(',',$this->ids);

         if(isset($val['children'])&&!empty($val['children'])){
       
            $data[$key]['children']=$this->ContaintChildren($val['children']);        
         }      
     }
     return $data;
  }
  /**
   * 获取所有子类的id递归
   */
 protected function ChildrenGui($data){    
        foreach ($data as $key => $val1) {
            # code...   
            $a=$this->ids;    
            $a[]=$val1['id'];
            $this->ids=$a;
            if(isset($val1['children'])&&!empty($val1['children'])){             
               $this->ChildrenGui($val1['children']);
            }         
        }
    }
}